package com.iwdnb.gkgz.common.prediction;

import java.util.Collection;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class PredictionStrategyFactory implements ApplicationContextAware {

    private static Collection<PredictionStrategy> predictionStrategyList;
    private static PredictionStrategy defaultPredictionStrategy = new DefaultPredictionStrategy();
    //private static PredictionStrategy defaultPredictionStrategy = new ThirdPredictionStrategy();

    public static PredictionStrategy getStrategy(String strategyName) {
        if (CollectionUtils.isNotEmpty(predictionStrategyList)) {
            for (PredictionStrategy strategy : predictionStrategyList) {
                if (strategy.getClass().getSimpleName().equals(strategyName)) {
                    return strategy;
                }
            }
        }
        return defaultPredictionStrategy;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        predictionStrategyList = applicationContext.getBeansOfType(PredictionStrategy.class).values();
    }
}
